# import sys
# from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QSplitter, QFrame
# from PyQt5.QtCore import Qt
# from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
# from matplotlib.figure import Figure
# import a
# import b
# import c  # 导入c.py
#
# # 定义一个类继承 FigureCanvas，用于在 PyQt5 中嵌入 Matplotlib 图形
# class MplCanvas(FigureCanvas):
#     def __init__(self, parent=None, width=5, height=4, dpi=100):
#         fig = Figure(figsize=(width, height), dpi=dpi)
#         self.axes = fig.add_subplot(111)
#         super(MplCanvas, self).__init__(fig)
#         self.ani = None  # 用于存储动画对象
#
# # 定义主窗口类
# class MainWindow(QWidget):
#     def __init__(self):
#         super().__init__()
#         self.initUI()  # 初始化界面
#
#     def initUI(self):
#         self.setWindowTitle('PyQt5 Matplotlib Example')
#
#         # 创建按钮
#         self.button1 = QPushButton('Button 1', self)
#         self.button2 = QPushButton('Button 2', self)
#         self.button3 = QPushButton('Button 3', self)  # 新增按钮3
#
#         # 绑定按钮点击事件
#         self.button1.clicked.connect(self.plot_a)
#         self.button2.clicked.connect(self.plot_b)
#         self.button3.clicked.connect(self.plot_c)  # 绑定按钮3的点击事件
#
#         # 创建一个垂直的布局用于放置按钮
#         self.buttonLayout = QVBoxLayout()
#         self.buttonLayout.addWidget(self.button1)
#         self.buttonLayout.addWidget(self.button2)
#         self.buttonLayout.addWidget(self.button3)  # 添加按钮3到布局
#
#         # 创建一个Frame来包含按钮布局
#         self.leftFrame = QFrame(self)
#         self.leftFrame.setLayout(self.buttonLayout)
#
#         # 创建绘图画布
#         self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
#
#         # 创建一个水平的splitter
#         self.splitter = QSplitter(Qt.Horizontal)
#         self.splitter.addWidget(self.leftFrame)
#         self.splitter.addWidget(self.canvas)
#
#         # 设置主布局
#         self.mainLayout = QVBoxLayout(self)
#         self.mainLayout.addWidget(self.splitter)
#
#         self.setLayout(self.mainLayout)
#
#     # 点击按钮1时调用的函数
#     def plot_a(self):
#         if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
#             self.canvas.ani.event_source.stop()  # 停止当前动画
#         self.canvas.axes.clear()  # 清除当前绘图内容
#         self.canvas.ani = a.bubble_sort_visualization(self.canvas.axes)  # 创建新动画
#         self.canvas.draw_idle()  # 刷新绘图画布
#
#     # 点击按钮2时调用的函数
#     def plot_b(self):
#         if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
#             self.canvas.ani.event_source.stop()  # 停止当前动画的事件源
#         self.canvas.axes.clear()  # 清除当前绘图内容
#         self.canvas.ani = b.tsp_visualization(self.canvas.axes)  # 创建新动画
#         self.canvas.draw_idle()  # 刷新绘图画布
#
#     # 点击按钮3时调用的函数
#     def plot_c(self):
#         if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
#             self.canvas.ani.event_source.stop()  # 停止当前动画
#         self.canvas.axes.clear()  # 清除当前绘图内容
#         self.canvas.ani = c.graph_coloring_visualization(self.canvas.axes)  # 创建新动画
#         self.canvas.draw_idle()  # 刷新绘图画布
#
# # 应用程序入口
# if __name__ == '__main__':
#     app = QApplication(sys.argv)
#     mainWin = MainWindow()
#     mainWin.show()
#     sys.exit(app.exec_())








#
# import sys
# from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QSplitter, QFrame
# from PyQt5.QtCore import Qt
# from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
# from matplotlib.figure import Figure
# import a
# import b
# import c
# import d  # 导入d.py
#
#
# # 定义一个类继承 FigureCanvas，用于在 PyQt5 中嵌入 Matplotlib 图形
# class MplCanvas(FigureCanvas):
#     def __init__(self, parent=None, width=5, height=4, dpi=100):
#         fig = Figure(figsize=(width, height), dpi=dpi)
#         self.axes = fig.add_subplot(111)
#         super(MplCanvas, self).__init__(fig)
#         self.ani = None  # 用于存储动画对象
#
#
# # 定义主窗口类
# class MainWindow(QWidget):
#     def __init__(self):
#         super().__init__()
#         self.initUI()  # 初始化界面
#
#     def initUI(self):
#         self.setWindowTitle('PyQt5 Matplotlib Example')
#
#         # 创建按钮
#         self.button1 = QPushButton('Button 1', self)
#         self.button2 = QPushButton('Button 2', self)
#         self.button3 = QPushButton('Button 3', self)
#         self.button4 = QPushButton('Button 4', self)  # 新增按钮4
#
#         # 绑定按钮点击事件
#         self.button1.clicked.connect(self.plot_a)
#         self.button2.clicked.connect(self.plot_b)
#         self.button3.clicked.connect(self.plot_c)
#         self.button4.clicked.connect(self.plot_d)  # 绑定按钮4的点击事件
#
#         # 创建一个垂直的布局用于放置按钮
#         self.buttonLayout = QVBoxLayout()
#         self.buttonLayout.addWidget(self.button1)
#         self.buttonLayout.addWidget(self.button2)
#         self.buttonLayout.addWidget(self.button3)
#         self.buttonLayout.addWidget(self.button4)  # 添加按钮4到布局
#
#         # 创建一个Frame来包含按钮布局
#         self.leftFrame = QFrame(self)
#         self.leftFrame.setLayout(self.buttonLayout)
#
#         # 创建绘图画布
#         self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
#
#         # 创建一个水平的splitter
#         self.splitter = QSplitter(Qt.Horizontal)
#         self.splitter.addWidget(self.leftFrame)
#         self.splitter.addWidget(self.canvas)
#
#         # 设置主布局
#         self.mainLayout = QVBoxLayout(self)
#         self.mainLayout.addWidget(self.splitter)
#
#         self.setLayout(self.mainLayout)
#
#     # 点击按钮1时调用的函数
#     def plot_a(self):
#         if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
#             self.canvas.ani.event_source.stop()  # 停止当前动画
#         self.canvas.axes.clear()  # 清除当前绘图内容
#         self.canvas.ani = a.bubble_sort_visualization(self.canvas.axes)  # 创建新动画
#         self.canvas.draw_idle()  # 刷新绘图画布
#
#     # 点击按钮2时调用的函数
#     def plot_b(self):
#         if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
#             self.canvas.ani.event_source.stop()  # 停止当前动画的事件源
#         self.canvas.axes.clear()  # 清除当前绘图内容
#         self.canvas.ani = b.tsp_visualization(self.canvas.axes)  # 创建新动画
#         self.canvas.draw_idle()  # 刷新绘图画布
#
#     # 点击按钮3时调用的函数
#     def plot_c(self):
#         if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
#             self.canvas.ani.event_source.stop()  # 停止当前动画
#         self.canvas.axes.clear()  # 清除当前绘图内容
#         self.canvas.ani = c.graph_coloring_visualization(self.canvas.axes)  # 创建新动画
#         self.canvas.draw_idle()  # 刷新绘图画布
#
#     # 点击按钮4时调用的函数
#     def plot_d(self):
#         if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
#             self.canvas.ani.event_source.stop()  # 停止当前动画
#         self.canvas.axes.clear()  # 清除当前绘图内容
#         self.canvas.ani = d.prims_algorithm_visualization(self.canvas.axes)  # 创建新动画
#         self.canvas.draw_idle()  # 刷新绘图画布
#
#
# # 应用程序入口
# if __name__ == '__main__':
#     app = QApplication(sys.argv)
#     mainWin = MainWindow()
#     mainWin.show()
#     sys.exit(app.exec_())



import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QSplitter, QFrame
from PyQt5.QtCore import Qt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import a
import b
import c
import d  # 导入d.py
import e  # 导入e.py中的方法

# 定义一个类继承 FigureCanvas，用于在 PyQt5 中嵌入 Matplotlib 图形
class MplCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)
        self.ani = None  # 用于存储动画对象

# 定义主窗口类
class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()  # 初始化界面

    def initUI(self):
        self.setWindowTitle('PyQt5 Matplotlib Example')

        # 创建按钮
        self.button1 = QPushButton('冒泡排序', self)
        self.button2 = QPushButton('tsp旅行商问题', self)
        self.button3 = QPushButton('图着色问题', self)
        self.button4 = QPushButton('prims最小生成树', self)  # 新增按钮4
        self.button5 = QPushButton('哈夫曼树问题', self)  # 新增按钮5

        # 绑定按钮点击事件
        self.button1.clicked.connect(self.plot_a)
        self.button2.clicked.connect(self.plot_b)
        self.button3.clicked.connect(self.plot_c)
        self.button4.clicked.connect(self.plot_d)  # 绑定按钮4的点击事件
        self.button5.clicked.connect(self.plot_e)  # 绑定按钮5的点击事件

        # 创建一个垂直的布局用于放置按钮
        self.buttonLayout = QVBoxLayout()
        self.buttonLayout.addWidget(self.button1)
        self.buttonLayout.addWidget(self.button2)
        self.buttonLayout.addWidget(self.button3)
        self.buttonLayout.addWidget(self.button4)  # 添加按钮4到布局
        self.buttonLayout.addWidget(self.button5)  # 添加按钮5到布局

        # 创建一个Frame来包含按钮布局
        self.leftFrame = QFrame(self)
        self.leftFrame.setLayout(self.buttonLayout)

        # 创建绘图画布
        self.canvas = MplCanvas(self, width=5, height=4, dpi=100)

        # 创建一个水平的splitter
        self.splitter = QSplitter(Qt.Horizontal)
        self.splitter.addWidget(self.leftFrame)
        self.splitter.addWidget(self.canvas)

        # 设置主布局
        self.mainLayout = QVBoxLayout(self)
        self.mainLayout.addWidget(self.splitter)

        self.setLayout(self.mainLayout)

    # 点击按钮1时调用的函数
    def plot_a(self):
        print(self.canvas.get_width_height())
        if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
            self.canvas.ani.event_source.stop()  # 停止当前动画
        self.canvas.axes.clear()  # 清除当前绘图内容
        self.canvas.ani = a.bubble_sort_visualization(self.canvas.axes)  # 创建新动画
        self.canvas.draw_idle()  # 刷新绘图画布

    # 点击按钮2时调用的函数
    def plot_b(self):
        if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
            self.canvas.ani.event_source.stop()  # 停止当前动画的事件源
        self.canvas.axes.clear()  # 清除当前绘图内容
        self.canvas.ani = b.tsp_visualization(self.canvas.axes)  # 创建新动画
        self.canvas.draw_idle()  # 刷新绘图画布

    # 点击按钮3时调用的函数
    def plot_c(self):
        if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
            self.canvas.ani.event_source.stop()  # 停止当前动画
        self.canvas.axes.clear()  # 清除当前绘图内容
        self.canvas.ani = c.graph_coloring_visualization(self.canvas.axes)  # 创建新动画
        self.canvas.draw_idle()  # 刷新绘图画布

    # 点击按钮4时调用的函数
    def plot_d(self):
        if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
            self.canvas.ani.event_source.stop()  # 停止当前动画
        self.canvas.axes.clear()  # 清除当前绘图内容
        self.canvas.ani = d.prims_algorithm_visualization(self.canvas.axes)  # 创建新动画
        self.canvas.draw_idle()  # 刷新绘图画布

    # 点击按钮5时调用的函数
    def plot_e(self):
        if self.canvas.ani is not None and self.canvas.ani.event_source is not None:
            self.canvas.ani.event_source.stop()  # 停止当前动画
        self.canvas.axes.clear()  # 清除当前绘图内容
        self.canvas.ani = e.huffman_tree_visual(self.canvas.axes)  # 调用e.py中的huffman_tree_visual方法展示动画
        self.canvas.draw_idle()  # 刷新绘图画布

# 应用程序入口
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())
